package model;
import java.util.ArrayList;
import java.util.List;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

import model.base.BaseEquipmentTypes;

@SuppressWarnings("serial")
public class EquipmentTypes extends BaseEquipmentTypes<EquipmentTypes> {
    public static final EquipmentTypes dao = new EquipmentTypes().dao();

	public List<EquipmentTypes> findByNameAndStationId(String name , Long stationId){
        return this.find(" SELECT tet.* FROM t_equipment_types tet WHERE tet.name = ? AND tet.stationId = ?  ORDER BY tet.createTime " , name , stationId);
    }

	public List<String> getOptionsList(Long stationId){
		List<Record> list = Db.find(" SELECT tet.name FROM t_equipment_types tet WHERE tet.stationId = ?  ORDER BY tet.createTime "  , stationId );
		List<String> options = new ArrayList<String>();
		for (Record record : list) {
			options.add(record.getStr("name"));
		}
		return options ;
    }
	
	public void save(Long stationId , String name) {
		EquipmentTypes types = this.findFirstByNameAndStationId(name, stationId);
		if (null == types) {
			types = new EquipmentTypes();
			types.setName(name);
			types.setStationId(stationId);
			types.save();
		}
	}

	public EquipmentTypes findFirstByNameAndStationId(String name , Long stationId){
        return this.findFirst(" SELECT tet.* FROM t_equipment_types tet WHERE tet.name = ? AND tet.stationId = ?  LIMIT 1 " , name , stationId);
    }
}
